﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2012 , gt TECH, Ltd.
//--------------------------------------------------------------------

using System;
using System.Data;

namespace UMPlatForm.Business
{
  using UMPlatForm.Business;
  

 /// <summary>
 /// PiPermissionManager
 /// 操作权限存储表
 ///
 /// 修改纪录
 ///
 ///		2012-03-02 版本：1.0 EricHu 创建主键。
 ///
 /// 版本：1.0
 ///
 /// <author>
 ///		<name>EricHu</name>
 ///		<date>2012-03-02</date>
 /// </author>
 /// </summary>
 public partial class PiPermissionManager : BaseManager, IBaseManager
 {
  /// <summary>
  /// 构造函数
  /// </summary>
  public PiPermissionManager()
  {
      base.CurrentTableName = PiPermissionTable.TableName;
      base.PrimaryKey = "Id";
  }

  /// <summary>
  /// 构造函数
  /// <param name="tableName">指定表名</param>
  /// </summary>
  public PiPermissionManager(string tableName)
  {
      base.CurrentTableName = tableName;
  }

  /// <summary>
  /// 构造函数
  /// </summary>
  /// <param name="dbHelper">数据库连接</param>
  public PiPermissionManager(IDbHelper dbHelper): this()
  {
      DbHelper = dbHelper;
  }

  /// <summary>
  /// 构造函数
  /// </summary>
  /// <param name="userInfo">用户信息</param>
  public PiPermissionManager(UserInfo userInfo) : this()
  {
      UserInfo = userInfo;
  }

  /// <summary>
  /// 构造函数
  /// </summary>
  /// <param name="dbHelper">数据库连接</param>
  /// <param name="userInfo">用户信息</param>
  public PiPermissionManager(IDbHelper dbHelper, UserInfo userInfo) : this(dbHelper)
  {
      UserInfo = userInfo;
  }

  /// <summary>
  /// 构造函数
  /// </summary>
  /// <param name="dbHelper">数据库连接</param>
  /// <param name="userInfo">用户信息</param>
  /// <param name="tableName">指定表名</param>
  public PiPermissionManager(IDbHelper dbHelper, UserInfo userInfo, string tableName) : this(dbHelper, userInfo)
  {
      base.CurrentTableName = tableName;
  }

  /// <summary>
  /// 添加
  /// </summary>
  /// <param name="piPermissionEntity">实体</param>
  /// <returns>主键</returns>
  public string Add(PiPermissionEntity piPermissionEntity)
  {
      return this.AddEntity(piPermissionEntity);
  }

  /// <summary>
  /// 添加
  /// </summary>
  /// <param name="piPermissionEntity">实体</param>
  /// <param name="identity">自增量方式</param>
  /// <param name="returnId">返回主键</param>
  /// <returns>主键</returns>
  public string Add(PiPermissionEntity piPermissionEntity, bool identity, bool returnId)
  {
      this.Identity = identity;
      this.ReturnId = returnId;
      return this.AddEntity(piPermissionEntity);
  }

  /// <summary>
  /// 更新
  /// </summary>
  /// <param name="piPermissionEntity">实体</param>
  public int Update(PiPermissionEntity piPermissionEntity)
  {
      return this.UpdateEntity(piPermissionEntity);
  }

  /// <summary>
  /// 获取实体
  /// </summary>
  /// <param name="id">主键</param>
  public PiPermissionEntity GetEntity(string id)
  {
      return GetEntity(int.Parse(id));
  }

  public PiPermissionEntity GetEntity(int id)
  {
      PiPermissionEntity piPermissionEntity = new PiPermissionEntity(this.GetDT(PiPermissionTable.FieldId, id));
      return piPermissionEntity;
  }

  /// <summary>
  /// 添加实体
  /// </summary>
  /// <param name="piPermissionEntity">实体</param>
  public string AddEntity(PiPermissionEntity piPermissionEntity)
  {
       string sequence = string.Empty;
      SQLBuilder sqlBuilder = new SQLBuilder(DbHelper, this.Identity, this.ReturnId);
      sqlBuilder.BeginInsert(this.CurrentTableName, PiPermissionTable.FieldId);
  if (!this.Identity) 
  {
      sqlBuilder.SetValue(PiPermissionTable.FieldId, piPermissionEntity.Id);
  }
  else
  {
  if (!this.ReturnId && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.DB2))
  {
      if (DbHelper.CurrentDbType == CurrentDbType.Oracle)
      {
          sqlBuilder.SetFormula(PiPermissionTable.FieldId, "SEQ_" + this.CurrentTableName.ToUpper() + ".NEXTVAL ");
      }
      if (DbHelper.CurrentDbType == CurrentDbType.DB2)
      {
          sqlBuilder.SetFormula(PiPermissionTable.FieldId, "NEXT VALUE FOR SEQ_" + this.CurrentTableName.ToUpper());
      }
  }
  else
  {
      if (this.Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.DB2))
      {
if (piPermissionEntity.Id == null)
{
    if (string.IsNullOrEmpty(sequence))
    {
        BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper, this.Identity);
        sequence = sequenceManager.GetSequence(this.CurrentTableName);
    }
    piPermissionEntity.Id = int.Parse(sequence);
}
        sqlBuilder.SetValue(PiPermissionTable.FieldId, piPermissionEntity.Id);
      }
  }
  }
      this.SetEntity(sqlBuilder, piPermissionEntity);
      if (UserInfo != null) 
      { 
      sqlBuilder.SetValue(PiPermissionTable.FieldCreateUserId, UserInfo.Id);
      sqlBuilder.SetValue(PiPermissionTable.FieldCreateBy, UserInfo.RealName);
      } 
      sqlBuilder.SetDBNow(PiPermissionTable.FieldCreateOn);
      if (UserInfo != null) 
      { 
      sqlBuilder.SetValue(PiPermissionTable.FieldModifiedBy, UserInfo.RealName);
      } 
      sqlBuilder.SetDBNow(PiPermissionTable.FieldModifiedOn);
    if (this.Identity && (DbHelper.CurrentDbType == CurrentDbType.SqlServer || DbHelper.CurrentDbType == CurrentDbType.Access))
    {
        sequence = sqlBuilder.EndInsert().ToString();
    }
    else
    {
        sqlBuilder.EndInsert();
    }
    return sequence;
  }

  /// <summary>
  /// 更新实体
  /// </summary>
  /// <param name="piPermissionEntity">实体</param>
  public int UpdateEntity(PiPermissionEntity piPermissionEntity)
  {
      SQLBuilder sqlBuilder = new SQLBuilder(DbHelper);
      sqlBuilder.BeginUpdate(this.CurrentTableName);
      this.SetEntity(sqlBuilder, piPermissionEntity);
      if (UserInfo != null) 
      { 
      sqlBuilder.SetValue(PiPermissionTable.FieldModifiedBy, UserInfo.RealName);
      } 
      sqlBuilder.SetDBNow(PiPermissionTable.FieldModifiedOn);
      sqlBuilder.SetWhere(PiPermissionTable.FieldId, piPermissionEntity.Id);
      return sqlBuilder.EndUpdate();
  }

  /// <summary>
  /// 设置实体
  /// </summary>
  /// <param name="piPermissionEntity">实体</param>
  private void SetEntity(SQLBuilder sqlBuilder, PiPermissionEntity piPermissionEntity)
  {
      sqlBuilder.SetValue(PiPermissionTable.FieldResourceCategory, piPermissionEntity.ResourceCategory);
      sqlBuilder.SetValue(PiPermissionTable.FieldResourceId, piPermissionEntity.ResourceId);
      sqlBuilder.SetValue(PiPermissionTable.FieldPermissionId, piPermissionEntity.PermissionId);
      sqlBuilder.SetValue(PiPermissionTable.FieldPermissionConstraint, piPermissionEntity.PermissionConstraint);
      sqlBuilder.SetValue(PiPermissionTable.FieldDescription, piPermissionEntity.Description);
      sqlBuilder.SetValue(PiPermissionTable.FieldModifyUserId, piPermissionEntity.ModifyUserId);
  }

  /// <summary>
  /// 删除实体
  /// </summary>
  /// <param name="id">主键</param>
  /// <returns>影响行数</returns>
  public int Delete(int id)
  {
      return this.Delete(PiPermissionTable.FieldId, id);
  }
  }
}
